package com.services;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.ProductType;
import com.dao.ProductTypeServices;
import com.impl.ProductTypeServicesImpl;
public class ProductTypeServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html");
		resp.setCharacterEncoding("utf-8");
		String option=req.getParameter("option");
		System.out.println(option);
		ProductTypeServices productTypeServices=new ProductTypeServicesImpl();
		if (null==option||"list".equals(option)) {
			List<ProductType>productTypes=productTypeServices.findByWhere(null,null);
			String msg= req.getParameter("msg");
			req.setAttribute("msg",msg);
			req.setAttribute("productTypes", productTypes);
			req.getRequestDispatcher("productType.jsp").forward(req,resp);
		}else if ("add".equals(option)) {
			ProductType productType= new ProductType();
			String productTypeName=req.getParameter("productTypeName");
			String sort=req.getParameter("sort");
			productType.setProcuctTypeName(productTypeName);
			productType.setSort(Integer.parseInt(sort));
			Map<String,Object>map=new HashMap<String, Object>();
			map=productTypeServices.save(productType);
			boolean flag=(boolean) map.get("flag");
			if (flag) {
				resp.sendRedirect("productType.html");
			}else{
				req.setAttribute("msg",map.get("massage"));
				req.getRequestDispatcher("productTypeadd.jsp").forward(req,resp);
			}
			
		}else if ("delete".equals(option)) {
			String id= req.getParameter("id");
			productTypeServices.delete(id);
			req.setAttribute("msg","删除成功");
			req.getRequestDispatcher("productType.html?option=list").forward(req,resp);
		}else if ("update".equals(option)) {
			ProductType productType= new ProductType();
			String productTypeId=req.getParameter("productTypeId");
			String productTypeName=req.getParameter("productTypeName");
			String sort=req.getParameter("sort");
			productType.setProcuctTypeName(productTypeName);
			productType.setSort(Integer.parseInt(sort));
			productType.setProductTypeId(Integer.parseInt(productTypeId));
			productTypeServices.update(productType);
			resp.sendRedirect("productType.html");
		}else if ("load".equals(option)) {
			String id= req.getParameter("id");
			ProductType productType=null;
			if (id!=null) {
				productType= productTypeServices.findByid(Integer.parseInt(id));
			}
			req.setAttribute("productType",productType);
			req.getRequestDispatcher("productTypeadd.jsp").forward(req,resp);
		}
	}
}
